use crate::service::model::chat_model::{Message, MessageList};

pub fn messages_to_insert_sql(messages: Vec<&Message>) -> String {
    let mut sql = "INSERT INTO public.message(
	user_id, value, mold, is_read, receive_id, create_time)
	VALUES ".to_string();

    let len = messages.len();
    for i in 0..len {
        let message = messages.get(i).unwrap();
        if i==0{
            sql.push_str(&format!("({0}, '{1}', {2}, {3}, {4}, '{5}')", message.user_id, message.value, message.mold, message.is_read, message.receive_id, message.create_time));
        }else {
            sql.push_str(&format!(" ,({0}, '{1}', {2}, {3}, {4}, '{5}')", message.user_id, message.value, message.mold, message.is_read, message.receive_id, message.create_time));
        }
    }
    sql.push(';');

    sql
}


pub fn message_list_to_insert_sql(message_list:Vec<&MessageList>)->String{
    let mut sql="INSERT INTO message_list(send_id, receive_id, num, last_message,last_time) VALUES ".to_string();

    let len = message_list.len();
    for i in 0..len {
        let message_list = message_list.get(i).unwrap();
        if i==0{
            sql.push_str(&format!("({0},{1}, {2},'{3}', '{4}')", message_list.send_id, message_list.receive_id, message_list.num, message_list.last_message, message_list.last_time));
        }else {
            sql.push_str(&format!(",({0},{1}, {2},'{3}', '{4}')", message_list.send_id, message_list.receive_id, message_list.num, message_list.last_message, message_list.last_time));
        }
    }
    sql.push_str("ON CONFLICT (send_id, receive_id) DO UPDATE SET num = EXCLUDED.num,
last_message =EXCLUDED.last_message,
last_time=EXCLUDED.last_time;");


    sql


}